var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
var mph = require("../../utils/util.js");
Page({
  data: {
    order_list_souhou:null,
    order_list_Data:null,
    order_id:'',
    goods_id:'',
    items: [
      { name: 'huanhuo', value: '换货', checked: 'true' },
      { name: 'tuihuo', value: '退货' }
    ],
    items_list: [
      { name: 'index1', value: '商品问题'},
      { name: 'index2', value: '物流问题' },
      { name: 'index3', value: '用户问题' }
    ],
    hide_shengqing_yuahyin:false,
    str_text:'请选择申请原因',
    refund_form_order: null,
    refund_form_goods: null,
    refund_form_reason_list: null,
    reason_id_:'',
    buyer_message:''
  },
  radioChangeAdd(e) {
//console.log('radio发生change事件，携带value值为：', e.detail.value)
  },
  radioChange: function (e) {
console.log('radio发生change事件，携带value值为：', e)
    let val = e.detail.value;
    let that = this;
    for (let i = 0; i < that.data.refund_form_reason_list.length;i++){
      if (that.data.refund_form_reason_list[i].reason_info == val){
        that.data.reason_id_ = that.data.refund_form_reason_list[i].reason_id;
        that.data.str_text = that.data.refund_form_reason_list[i].reason_info;
        that.setData({
          reason_id_: that.data.reason_id_,
          str_text: that.data.str_text,
        })
      }
    }

  },
  hide_shengqing_yuahyin(){
    let that = this;
    that.data.hide_shengqing_yuahyin = false;
    that.setData({
      hide_shengqing_yuahyin: that.data.hide_shengqing_yuahyin
    })
  },
  show_shengqing_yuahyin() {
    let that = this;
    that.data.hide_shengqing_yuahyin = true;
    that.setData({
      hide_shengqing_yuahyin: that.data.hide_shengqing_yuahyin
    })
  },
  // go_order_detail() {
  //   wx.navigateTo({
  //     url: '/pages/order_detail/order_detail'
  //   })
  // },
  uploadimg: function () {
        var that = this;
        wx.chooseImage({  //从本地相册选择图片或使用相机拍照
            count: 2, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片，批量添加的话，就是一个图片地址数组
                var tempFilePaths = res.tempFilePaths
                wx.setStorageSync('tempFilePaths', res.tempFilePaths)
                  that.setData({
                      sourcess: res.tempFilePaths  //这个用于展示添加的图片，并非服务器图片地址
                  })

        //添加图片后，会弹窗提示，确认是否上传
                wx.showModal({
                    title: '提示',
                    content: '确认上传这些吗',
                    success: function (res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                            var tempFilePaths = wx.getStorageSync('tempFilePaths')
                            var sources = [];
              //循环将添加的图片上传到服务器
                            for (var i = 0; i < tempFilePaths.length; i++) {
                                wx.showToast({
                                    title: '正在上传中',
                                    icon: 'loading',
                                    duration: 15000
                                })
                                wx.uploadFile({
                                  url: ApiUrl+'/imgupload.php?imgpath=userimg',
                                    filePath: tempFilePaths[i],
                                    name: 'file',
                                    success: function (res) {
                                        wx.hideToast();
                                        console.log(res.data)
                                        if (res.data !== "上传错误") {
                                            sources.push(res.data)
                                            //前台显示
                                            that.setData({
                                                sources: sources
                                            })
                                            console.log(sources)
                                        }
                                    }
                                })
                            }
                        } else if (res.cancel) {
                            console.log('用户点击取消')
                            that.setData({
                                sourcess: "",
                            })
                        }
                    }
                })
            }
        })
    },
  //退款信息
  //index.php?act=member_refund&op=refund_form
  refund_form() {
    var that = this;
    let k = wx.getStorageSync('key');
    console.log(that.data.order_id, that.data.rec_id);
    var url = 'index.php?act=member_refund&op=refund_form&order_id=' + that.data.order_id + '&order_goods_id=' + that.data.rec_id;
    let list = {
      key: k
    }
    console.log(k, that.data.order_id, that.data.goods_id);
    mph.postRequest(url, list, function (res) {
      if (res.data.code == 200) {
        console.log(res);
        let newarr = res.data.datas;
        that.setData({
          refund_form_order: newarr.order,
          refund_form_goods: newarr.goods,
          refund_form_reason_list: newarr.reason_list_arr
        })
      }
    });
  },
  add_textarea_txt(e){
    console.log(e.detail.value);
    let txt = e.detail.value;
    this.setData({
      buyer_message: txt
    })
  },
  //index.php?act=member_refund&op=refund_post
  refund_post(){
    var that = this;
    let k = wx.getStorageSync('key');
    var url = 'index.php?act=member_refund&op=refund_post'
    let list = {
      key: k,
      order_id: that.data.refund_form_order.order_id,
      order_goods_id: that.data.refund_form_goods.order_goods_id,
      refund_amount: that.data.refund_form_goods.goods_pay_price,
      goods_num: that.data.refund_form_goods.goods_num,
      reason_id: that.data.reason_id_,//原因id
      buyer_message: that.data.buyer_message,//申请原因
      refund_type:'1',
      refund_pic:[]//图片最多三个
    }
    console.log(k, that.data.order_id, that.data.refund_form_goods.order_goods_id, that.data.reason_id_);
    mph.postRequest(url, list, function (res) {
      if (res.data.code == 200) {
        console.log(res);
        // wx.redirectTo({
        //   url: '/pages/order_after_sale/order_after_sale'
        // }
        let newarr = res.data.datas;
        // that.setData({
        // })
        wx.redirectTo({
          url: '/pages/my_order/my_order'
        })
      }
    });
  },
  onLoad: function (option) {
    let that = this;
    console.log(option.rec_id);


    let order_list_souhou = wx.getStorageSync('order_list_souhou');
    console.log(order_list_souhou)
    if (option.rec_id){

      that.setData({
        order_id: option.order_id,
        rec_id: option.rec_id
      })
      that.refund_form();
    }
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})